package com.itheima.test4;

import com.itheima.test3.Student;
import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Author:gy
 * @Date: 2025/09/06 10:53
 * @Description:
 */
@SuppressWarnings("all")
public class Demo {
    Class clazz;
    @Before
    public void getClazz() throws ClassNotFoundException {
         clazz = Class.forName("com.itheima.test3.Student");
    }
    @Test
    public void method() throws Exception, IllegalAccessException {
        Constructor constructor = clazz.getConstructor();
        Student s = (Student) constructor.newInstance();
        System.out.println(s);
    }
    @Test
    public void method1() throws Exception, IllegalAccessException {
        Constructor constructor = clazz.getDeclaredConstructor(String.class);
        //就算是私有 要可以在其他类执行 打破封装和私有 暴力反射 暴力访问  取消检查
        constructor.setAccessible(true);
        Student s = (Student) constructor.newInstance("张三");
        System.out.println(s);
    }
    @Test
    public void method2() throws Exception, IllegalAccessException {
        Object o = clazz.newInstance();
        System.out.println(o);
    }
}
